/**
 * Copyright © 2008, EMC Corporation.
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 *     + Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     + Redistributions in binary form must reproduce the above copyright 
 *       notice, this list of conditions and the following disclaimer in the 
 *       documentation and/or other materials provided with the distribution.
 *     + The name of EMC Corporation may not be used to endorse or promote 
 *       products derived from this software without specific prior written 
 *       permission.
 *
 *      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 *      "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
 *      TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 *      PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
 *      BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 *      CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 *      SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 *      INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *      CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 *      ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 *      POSSIBILITY OF SUCH DAMAGE.
 */
package com.emc.esu.api
{
	import com.emc.esu.api.hash.MessageDigest;
	import com.emc.esu.api.hash.SHA0;
	import com.hurlant.math.BigInteger;
	
	import flash.utils.ByteArray;
	
	
	public class Checksum
	{
		/**
		 * The hash algorithm to use.  As of 1.4.0, only SHA0 is supported
		 */
		public static const ALGORITHM_SHA0:String = "SHA0";
		public static const ALGORITHM_SHA1:String = "SHA1";
		public static const ALGORITHM_MD5:String = "MD5";
		
		private var digest:MessageDigest;
		private var alg:String;
		private var offset:Number;
		private var expectedValue:String;
		
		public function Checksum( alg:String ) {
			if( alg != ALGORITHM_SHA0 ) {
				throw new ArgumentError( "Only SHA0 is currently supported" );
			}
			this.alg = alg;
			digest = new SHA0();
			offset = 0;
		}
		
		public function getAlgorithmName():String {
			return alg;
		}
		
		/**
		 * Updates the checksum with the given buffer's contents
		 * @param buffer data to update
		 */
		public function update( buffer:ByteArray ):void {
			digest.hashUpdate( buffer );
			this.offset += buffer.length;
		}
	
		/**
		 * Outputs the current digest checksum in a format
		 * suitable for including in Atmos create/update calls.
		 */
		public function toString():String {
			
			var checksumData:String = getAlgorithmName()+"/"+offset+"/"+getHashValue();
			trace( "Checksum Value: '" + checksumData + "'" );
			
			return checksumData;
		}
	
		private function getHashValue():String {
			// Clone the digest so we can pad current value for output
			var tmpDigest:MessageDigest;
			tmpDigest = digest.clone();
			
			var currDigest:ByteArray = tmpDigest.hashFinal(null);
			
			var bigInt:BigInteger = new BigInteger(currDigest, 0, true);
			return bigInt.toString(16);
		}
	
		/**
		 * Sets the expected value for this checksum.  Only used for read operations
		 * @param expectedValue the expectedValue to set
		 */
		public function setExpectedValue( expectedValue:String ):void {
			this.expectedValue = expectedValue;
		}
	
		/**
		 * Gets the expected value for this checksum.  Only used for read operations.
		 * @return the expectedValue
		 */
		public function getExpectedValue():String {
			return expectedValue;
		}
		
	}

}